package com.auto.api.framework.filter;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Description: 顶层过滤器集合
 *
 * @author liangshf
 * @date 2019年7月29日
 */
public abstract class TopFilterChain<T,S> implements TopFilter<T,S> {

	protected List<TopFilter<T,S>> filters = Lists.newArrayList();

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 新增一个过滤过滤器
	 * @param addFilter TopRule<T>
	 * @return
	 */
	public List<TopFilter<T,S>> addFilter(TopFilter<T,S>  addFilter) {
		filters.add(addFilter);
		return filters;
	}
	
	/**
	 * 新增一个过滤过滤器列表
	 * @param filtersList List<TopFilter<T>>
	 * @return
	 */
	public List<TopFilter<T,S>> addFilterList(List<TopFilter<T,S>> filtersList) {
		filters.addAll(filtersList);
		return filters;
	}
	
	/**
	 * 执行一个过滤过滤器列表
	 * @param t
	 * @return
	 */
	public int doFilter(T t , S s) {
		int result =0;
		for (TopFilter filter : filters) {
			int temp = filter.doFilter(t , s);
			result += temp;
		}
		return result == filters.size() ? 1 : -1;
	}
}
